package plugins.yzhkof.pv3d.control
{
    import core.manager.*;
    import core.manager.modelManager.*;
    import core.manager.pluginManager.*;
    import core.manager.popupManager.*;
    import core.manager.sceneManager.*;
    import core.manager.scriptManager.*;
    
    import flash.display.*;
    import flash.events.*;
    
    import memory.*;
    
    import mx.core.*;
    
    import org.papervision3d.cameras.*;
    
    import plugins.yzhkof.pv3d.view.*;
    
    import view.*;
    import view.fl2mx.Fl2Mx;
    import view.pv3d.*;
    import view.pv3d.event.*;

    public class Pv3dSceneCtr extends UIComponent
    {
        private var currentSceneXml:XML;
        private var controler:CameraRotationControler;
        private var controlerCompleteScript:String = "";
        private var pv3d:Pv3d360SceneCompass;
        private var updata_type:String;

        public function Pv3dSceneCtr()
        {
            pv3d = new Pv3d360SceneCompass();
            initScript();
            initListener();
            return;
        }// end function

        private function setCameraRotationX(_TitleWindowStyle__embed_css_Assets_swf_CloseButtonUp_964153841:Number) : void
        {
            if (controler != null)
            {
                controler.setRotation(_TitleWindowStyle__embed_css_Assets_swf_CloseButtonUp_964153841 - get360Camera().rotationX, get360Camera().rotationY);
            }
            else
            {
                get360Camera().rotationX = _TitleWindowStyle__embed_css_Assets_swf_CloseButtonUp_964153841;
            }
            return;
        }// end function

        private function setCameraRotationY(_TitleWindowStyle__embed_css_Assets_swf_CloseButtonUp_964153841:Number) : void
        {
            if (controler != null)
            {
                controler.setRotation(get360Camera().rotationX, _TitleWindowStyle__embed_css_Assets_swf_CloseButtonUp_964153841 - get360Camera().rotationY);
            }
            else
            {
                get360Camera().rotationY = _TitleWindowStyle__embed_css_Assets_swf_CloseButtonUp_964153841;
            }
            return;
        }// end function

        private function onCameraUpdata(_TitleWindowStyle__embed_css_Assets_swf_CloseButtonUp_964153841:Event) : void
        {
            pv3d.stage.quality = StageQuality.LOW;
            pv3d.render_type = Pv3d360Scene.REND_ALL;
            return;
        }// end function

        private function updataHotPoints() : void
        {
            var _loc_2:Boolean;
            var _loc_1:* = currentSceneXml.Animate;
            pv3d.cleanAllAnimate();
            var _loc_3:int;
            while (_loc_3 < _loc_1.length())
            {
                
                _loc_2 = _loc_1[_loc_3].@cache == 1 ? (true) : (false);
                pv3d.addAminate(_loc_1[_loc_3].@url, {x:_loc_1[_loc_3].@x, y:_loc_1[_loc_3].@y, z:_loc_1[_loc_3].@z, rotationX:_loc_1[_loc_3].@rotationX, rotationY:_loc_1[_loc_3].@rotationY, rotationZ:_loc_1[_loc_3].@rotationZ, width:_loc_1[_loc_3].@width, height:_loc_1[_loc_3].@height, segmentsW:_loc_1[_loc_3].@segmentsW, segmentsH:_loc_1[_loc_3].@segmentsH, offset:_loc_1[_loc_3].@offset, angle:_loc_1[_loc_3].@angle, force:_loc_1[_loc_3].@force, onClick:_loc_1[_loc_3].@onClick, visible:_loc_1[_loc_3].@visible, tip:_loc_1[_loc_3].@tip,tipEnglish:_loc_1[_loc_3].@tipEnglish, scaleX:_loc_1[_loc_3].@scaleX, scaleY:_loc_1[_loc_3].@scaleY, movement:_loc_1[_loc_3].@movement, speed:_loc_1[_loc_3].@speed, maxHeight:_loc_1[_loc_3].@maxHeight, minHeight:_loc_1[_loc_3].@minHeight, filter:_loc_1[_loc_3].@filter, sign:_loc_1[_loc_3].@sign, debuge:_loc_1[_loc_3].@debuge, autoKeep:_loc_1[_loc_3].@autoKeep, onOver:_loc_1[_loc_3].@onOver, onOut:_loc_1[_loc_3].@onOut}, _loc_2);
                _loc_3++;
            }
            return;
        }// end function

        private function disable360System() : void
        {
            if (pv3d.parent != null)
            {
                pv3d.parent.removeChild(pv3d);
            }
            stopRender();
            return;
        }// end function

        private function startRender() : void
        {
            pv3d.startRend();
            return;
        }// end function

        private function enable360System() : void
        {
            if (pv3d.parent == null)
            {
                PluginManager.getInstance().getFloor(0).addChild(Fl2Mx.fl2Mx(pv3d));
            }
            if (controler == null)
            {
                AddToStageSetter.delayExcuteAfterAddToStage(pv3d, function () : void
            {
                controler = new CameraRotationControler(pv3d, get360Camera());
                controler.addEventListener(CamereaControlerEvent.UPDATE, onCameraUpdata);
                controler.addEventListener(CamereaControlerEvent.UPDATED, onCameraUpdataed);
                ScriptManager.getInstance().addApi(ScriptName.SET_GOTO_ROTATION, controler.setGotoRotation);
                return;
            }// end function
            );
            }
            startRender();
            return;
        }// end function

        private function get360Camera() : FreeCamera3D
        {
            return pv3d.camera;
        }// end function

        private function initControlerCompleteScript(_TitleWindowStyle__embed_css_Assets_swf_CloseButtonUp_964153841:String) : void
        {
            controlerCompleteScript = controlerCompleteScript + _TitleWindowStyle__embed_css_Assets_swf_CloseButtonUp_964153841;
            return;
        }// end function

        private function initListener() : void
        {
            CustomPopupManager.getInstance().addEventListener(PopupManagerEvent.SHOW_POPUP, function (_TitleWindowStyle__embed_css_Assets_swf_CloseButtonUp_964153841:PopupManagerEvent) : void
            {
                pv3d.stopRend();
                return;
            }// end function
            );
            CustomPopupManager.getInstance().addEventListener(PopupManagerEvent.REMOVE_POPUP, function (_TitleWindowStyle__embed_css_Assets_swf_CloseButtonUp_964153841:PopupManagerEvent) : void
            {
                pv3d.startRend();
                return;
            }// end function
            );
            return;
        }// end function

        private function setCamera(x:Number, y:Number) : void
        {
            MainSystem.getInstance().runAPIDirectDirectly(ScriptName.SET_GOTO_ROTATION, [x - get360Camera().rotationX, y - get360Camera().rotationY]);
            return;
        }// end function

        private function onCameraUpdataed(e:Event) : void
        {
            pv3d.stage.quality = StageQuality.HIGH;
            switch(updata_type)
            {
                case "movieclip":
                {
                    pv3d.render_type = Pv3d360Scene.REND_ALL;
                    break;
                }
                default:
                {
                    pv3d.draw();
                    pv3d.render_type = Pv3d360Scene.REND_ANIMATE;
                    break;
                    break;
                }
            }
            if (controlerCompleteScript != null)
            {
                ScriptManager.getInstance().runScriptDirectly(controlerCompleteScript);
                controlerCompleteScript = "";
            }
            return;
        }// end function

        private function gotoScene(id:int) : void
        {
            var id:* = id;
            enable360System();
            currentSceneXml = ModelManager.getInstance().xmlPv3d.Scene[id];
            pv3d.removeAllObject();
            var onComplete:* = function () : void
            {
                updataArrows();
                updataHotPoints();
                updateShpereAddon();
                SceneManager.getInstance().dispacherJustBeforeCompleteEvent(SceneManager.getInstance().currentSceneId);
                pv3d.draw();
                MyGC.gc();
                return;
            }// end function
            ;
            var url:* = String(currentSceneXml.@materialPath);
            var type:* = String(currentSceneXml.@materialType);
            updata_type = type;
            pv3d.changeBitmap(url, type, onComplete);
            return;
        }// end function

        private function setCameraFocus(_TitleWindowStyle__embed_css_Assets_swf_CloseButtonUp_964153841:Number) : void
        {
            get360Camera().focus = _TitleWindowStyle__embed_css_Assets_swf_CloseButtonUp_964153841;
            return;
        }// end function

        private function updataArrows() : void
        {
            var _loc_1:* = currentSceneXml.Compass;
            pv3d.cleanAllArrow();
            var _loc_2:int;
            while (_loc_2 < _loc_1.Arrow.length())
            {
                
                pv3d.addArrow(_loc_1.Arrow[_loc_2].@destination, _loc_1.Arrow[_loc_2].@rotation, _loc_1.Arrow[_loc_2].@tip,_loc_1.Arrow[_loc_2].@tipEnglish);
                _loc_2++;
            }
            return;
        }// end function

        private function stopRender() : void
        {
            pv3d.stopRend();
            return;
        }// end function

        private function updateShpereAddon() : Boolean
        {
            pv3d.cleanAllAddon();
            var _loc_1:* = currentSceneXml.ShpereAddon;
            if (_loc_1.length() == 0)
            {
                SceneManager.getInstance().dispacherJustBeforeCompleteEvent(SceneManager.getInstance().currentSceneId);
                return false;
            }
            pv3d.updateAddons(_loc_1);
            return true;
        }// end function

        private function initScript() : void
        {
            ScriptManager.getInstance().addApi(ScriptName.DISABLE_360_SYSTEM, disable360System);
            ScriptManager.getInstance().addApi(ScriptName.ENABLE_360_SYSTEM, enable360System);
            ScriptManager.getInstance().addApi(ScriptName.GET_CAMERA, get360Camera);
            ScriptManager.getInstance().addApi(ScriptName.GOTO_3D_SCENE, gotoScene);
            ScriptManager.getInstance().addApi(ScriptName.START_RENDER, startRender);
            ScriptManager.getInstance().addApi(ScriptName.STOP_RENDER, stopRender);
            ScriptManager.getInstance().addApi(ScriptName.SET_CAMERA, setCamera);
            ScriptManager.getInstance().addApi(ScriptName.SET_CAMERA_ROTATION_X, setCameraRotationX);
            ScriptManager.getInstance().addApi(ScriptName.SET_CAMERA_ROTATION_Y, setCameraRotationY);
            ScriptManager.getInstance().addApi(ScriptName.SET_CAMERA_FOCUS, setCameraFocus);
            ScriptManager.getInstance().addApi(ScriptName.ADD_CONTROLER_COMPLETE_SCRIPT, initControlerCompleteScript);
            return;
        }// end function

    }
}
